/**
 iTextUploader
 (C) 2008 MyselftDev (myselftdev@gmail.com)
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; version 2
 of the License.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

/* Revision: $Id: ITUImport.h 63 2009-02-07 16:28:48Z myselftdev $ */

#import <Foundation/Foundation.h>
#import <CoreFoundation/CoreFoundation.h>
#import <AddressBook/AddressBook.h>
#import <sqlite3.h>

#import "Definitions.h"
#import "PreferencesData.h"

#import "ASIFormDataRequest.h"
#import "ASINetworkQueue.h"
#import "SBJSON.h"

#define kImportModeMessages					1
#define kImportModeAddressBook				2

#define kImportStatusAddressBookNoNewElements	-2
#define kImportComplete						-1
#define kImportError						0

#define kImportStatusStarting				1
#define kImportStatusLoadingAddressBook		2
#define kImportStatusReceivedAddressBook	3
#define kImportStatusPreparingData			4
#define kImportStatusUploading				5

@interface ITUImport : NSObject {
	char				mode;
	
	id					delegate;
	NSString			*username;
	NSString			*password;
	NSUInteger			lastId;
	id					uploadProgressDelegate;
	
	NSDictionary		*returnData;
	
	NSOperationQueue	*queue;
	
	NSMutableArray		*ABArray;
	NSMutableArray		*ABAdd;
	NSUInteger			lastROWID;
}

- (void) startRequest;
- (void) startAddressBookImport;

// Internal methods
- (void) loadAddressBook;
- (void) addressBookLoaded: (ASIHTTPRequest *)theRequest;
- (void) prepareData;
- (void) uploadData: (NSData*)data minDate:(NSUInteger)minDate maxDate:(NSUInteger)maxDate;
- (void) uploadDone: (ASIHTTPRequest *)theRequest;

- (void) prepareAddressBook;
- (void) uploadAddressBookData: (NSArray*)data;
- (NSString*) nameForAddresses: (NSArray*)addr;

- (NSUInteger) findNumber: (NSString*)number inAddressBook: (NSMutableArray *)array;
- (NSString *) escapeString:(NSString*)theString;

- (void) connectionError: (ASIHTTPRequest *)request;
- (void) throwError: (NSString*)error;

@property (nonatomic, retain)	id			delegate;
@property (nonatomic, retain)	NSString*	username;
@property (nonatomic, retain)	NSString*	password;
@property (assign)				NSUInteger	lastId;
@property (nonatomic, retain)	id			uploadProgressDelegate;

@property (nonatomic, retain)	NSDictionary* returnData;

@end

// Just to avoid some warnings :)
CFStringRef CPPhoneNumberCopyActiveCountryCode();
const char* CPPhoneNumberCopyNormalized(const char *str);
NSUInteger CPPhoneNumbersEqualStrict(const char* number,const char* addr);
NSUInteger CPPhoneNumbersEqual(const char* number,const char* addr, CFStringRef country);
